#include "DraggableLabel.h"
#include <QEvent>
#include <QDebug>

DraggableLabel::DraggableLabel(QMainWindow *parent)
    : QLabel(parent), _parent(parent), dragging(false), hasEntered(false), hasLeft(true) // 初始化标志位
{
}

void DraggableLabel::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
    {
        dragging = true;
        offset = event->globalPos() - _parent->pos();
        event->accept();
    }
}

void DraggableLabel::mouseMoveEvent(QMouseEvent *event)
{
    if (dragging)
    {
        QPoint newPos = event->globalPos() - offset;
        _parent->move(newPos);
        event->accept();
    }
}

void DraggableLabel::mouseReleaseEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
    {
        dragging = false;
        event->accept();
    }
}

void DraggableLabel::enterEvent(QEvent *event)
{
    if (!hasEntered) // 确保第一次进入时发出信号
    {
        emit mouseEntered(); // 发送进入信号
        hasEntered = true;   // 更新标志位
        hasLeft = false;     // 重置离开标志位
    }
    QLabel::enterEvent(event); // 调用基类实现
}

void DraggableLabel::leaveEvent(QEvent *event)
{
    if (!hasLeft) // 确保第一次离开时发出信号
    {
        emit mouseLeft();   // 发送离开信号
        hasLeft = true;     // 更新标志位
        hasEntered = false; // 重置进入标志位
    }
    QLabel::leaveEvent(event); // 调用基类实现
}